package com.glodon.paas.document.service;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.glodon.paas.document.DocumentBaseIT;
import com.glodon.paas.exception.NotFoundException;
import com.glodon.paas.exception.PrivilegeException;
import com.glodon.paas.exception.i18n.ErrorCode;
import com.glodon.paas.mail.SpringMailSender;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:test-document-service.xml", "classpath:paas-document-dao-mysql.xml", })
public class ShareServiceMySqlIT extends DocumentBaseIT {

    @Autowired
    private ShareService service;

    @Autowired
    private FileService  fileService;

    // @Before
    // public void setup(){
    // if(!initedData){
    // super.setup();
    // initedData = true;
    // }
    // SpringMailSender sender = Mockito.mock(SpringMailSender.class);
    // service.setMailSender(sender);
    // }

    @Test
    public void testShareService() {
        super.setup();
        testGetFile();
        testGetFiles();
        testGetRootFiles();
        testGetNavigation();
        testCancelShare();
        cleanShare();
        cleanFile();
    }

    public void testGetFile() {
        cleanShare();
        try {
            fileService.getFile(userId, fileId1, false);
        } catch (PrivilegeException e) {
            assertEquals(ErrorCode.PRIVILEGE_ERROR, e.getErrorCode());
        }
        createShare(1);

        assertEquals(fileService.getFile(userId, fileId1, false).getId(), fileId1);
        assertEquals(fileService.getFile(userId, fileIdPre + 3, false).getId(), fileIdPre + 3);
        assertEquals(fileService.getFile(ownerId, fileId1, false).getId(), fileId1);
    }

    public void testGetFiles() {
        cleanShare();
        try {
            fileService.getFiles(userId, fileId1, null, null, false);
        } catch (PrivilegeException e) {
            assertEquals(ErrorCode.PRIVILEGE_ERROR, e.getErrorCode());
        }

        createShare(1);

        assertEquals(fileService.getFiles(userId, fileId1, null, null, false).size(), 1);
    }

    public void testGetRootFiles() {
        cleanShare();
        assertEquals(service.getSharesForMe(userId).size(), 0);
        assertEquals(service.getSharesByMe(userId).size(), 0);
        createShare(1);
        assertEquals(service.getSharesForMe(userId).size(), 1);
        assertEquals(service.getSharesByMe(ownerId).size(), 1);
    }

    public void testCancelShare() {
        cleanShare();
        createShare(1);
        createShare(2);
        assertEquals(fileService.getFile(userId, fileIdPre + 2, false).getId(), fileIdPre + 2);
        service.cancelShare(ownerId, fileIdPre + 2, userId);
        try {
            fileService.getFile(userId, fileIdPre + 2, false);
        } catch (PrivilegeException e) {
            assertEquals(ErrorCode.PRIVILEGE_ERROR, e.getErrorCode());
        }
        assertEquals(fileService.getFile(userId, fileIdPre + 3, false).getId(), fileIdPre + 3);
        try {
            service.cancelShare(userId, fileIdPre + 3);
        } catch (NotFoundException e) {
            assertEquals(ErrorCode.NOT_FOUND, e.getErrorCode());
        }
        service.cancelShare(userId, fileIdPre + 1);
        try {
            fileService.getFile(userId, fileIdPre + 3, false);
        } catch (PrivilegeException e) {
            assertEquals(ErrorCode.PRIVILEGE_ERROR, e.getErrorCode());
        }
    }

    public void testGetNavigation() {
        cleanShare();
        createShare(1);
        createShare(2);
        assertEquals(service.getNavigationByMe(ownerId, fileIdPre + 3).size(), 2);
        assertEquals(service.getNavigationByMe(fileIdPre + 1, fileIdPre + 3).size(), 2);
        assertEquals(service.getNavigationForMe(userId, fileIdPre + 3).size(), 2);
        assertEquals(service.getNavigationForMe(userId, fileIdPre + 2).size(), 1);
    }

    // @Test
    // public void testShareFile(){
    // removeShare(1);
    // try {
    // service.shareFile(ownerId, fileId1, "1@1.com", "read");
    // } catch (Exception e) {
    // System.out.println(e.getMessage());
    // }
    // }

    // @Test
    // public void removeData(){
    // if(initedData){
    // cleanFile();
    // cleanShare();
    // initedData = false;
    // }
    // }
}
